﻿using Decision.BusinessLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Telerik.Web.UI.GridExcelBuilder;

public partial class report_Report2_2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
        }
    }

    private void InitPage()
    {
        Reset_Click(null, null);
    }

    protected override void OnInit(EventArgs e)
    {
        InitValidation();
        base.OnInit(e);
    }

    private void BindReport()
    {
        //Validate criteria
        if (cal_startDate.SelectedDate == null || cal_endDate.SelectedDate == null)
        {
            return;
        }

        List<ReportDecision1_2> listReport = DCSFactory.CreateReportDecision1_2Factory().SelectByBusinessObject(new ReportDecision1_2Criteria
        {
            StartDate = cal_startDate.SelectedDate,
            EndDate = cal_endDate.SelectedDate,
            VOTE_TYPE_ID = "2",//ส.ว.
            NOTICE = "2",//หลัง
            VOTE_CASE_ID = "2,3"//แบบ เขตเลือกตั้ง,สรรหา
        });

        BindReport(listReport);
    }

    /// <summary>
    /// Init Validate RequireField in Controll
    /// </summary>
    private void InitValidation()
    {
        Validation.ValidateRequiredField(ref pnSearch, new Control[] { cal_startDate, cal_endDate }, Constant.VALIDATE_GROUP_SEARCH);
    }
    protected void Search_Click(object sender, EventArgs e)
    {
        BindReport();
    }
    protected void RadGrid1_ExcelMLWorkBookCreated(object sender, Telerik.Web.UI.GridExcelBuilder.GridExcelMLWorkBookCreatedEventArgs e)
    {
        foreach (RowElement row in e.WorkBook.Worksheets[0].Table.Rows)
        {
            row.Cells[0].StyleValue = "Style1";
        }

        StyleElement style = new StyleElement("Style1");
        style.InteriorStyle.Pattern = InteriorPatternType.Solid;
        style.InteriorStyle.Color = System.Drawing.Color.LightGray;
        e.WorkBook.Styles.Add(style);
    }

    protected void ImageButton_Click(object sender, ImageClickEventArgs e)
    {
        string alternateText = (sender as ImageButton).AlternateText;
        ConfigureExport();
        RadGrid1.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.Html;

        RadGrid1.MasterTableView.ExportToExcel();
    }

    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        ConfigureExport();
    }

    public void ConfigureExport()
    {
        RadGrid1.ExportSettings.FileName = DateTime.Now.ToFileTimeUtc() + "";
        RadGrid1.ExportSettings.ExportOnlyData = true;
        RadGrid1.ExportSettings.IgnorePaging = false;
        RadGrid1.ExportSettings.OpenInNewWindow = true;
    }
    protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = ViewState["listReport"];
    }
    protected void Reset_Click(object sender, EventArgs e)
    {
        cal_startDate.SelectedDate = null;
        cal_endDate.SelectedDate = null;

        BindReport(null);
    }

    private void BindReport(List<ReportDecision1_2> listReport)
    {
        ViewState["listReport"] = listReport;

        bool display = listReport != null && listReport.Count > 0;

        if (display)
        {
            Util.jsWriter(Page, "$('#exportXLS').show();");
        }
        else
        {
            Util.jsWriter(Page, "$('#exportXLS').hide();");
        }

        RadGrid1.Visible = display;
        RadGrid1.DataSource = listReport;
        RadGrid1.VirtualItemCount = listReport == null || listReport.Count == 0 ? 0 : (int)listReport[0].RowAll;
        RadGrid1.DataBind();
    }
}